package cn.school.controller.workflow;

import cn.school.common.annotation.SysLog;
import cn.school.common.api.CommonPage;
import cn.school.common.api.CommonResult;
import cn.school.common.exception.ApiException;
import cn.school.workflow.service.MaterialCollectService;
import cn.school.workflow.vo.MaterialCollectVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/collect")
@Api(tags = "物品申领接口")
public class MaterialCollectController {

    @Autowired
    private MaterialCollectService materialCollectService;

    @PostMapping("/list")
    @ApiOperation("list => 分页查询物品申请列表")
    public CommonResult<CommonPage<MaterialCollectVO>> findMaterialCollectPageList(@RequestBody MaterialCollectVO vo) throws ApiException {
        IPage<MaterialCollectVO> pageList =  materialCollectService.findMaterialCollectPageList(vo);
        return  CommonResult.success(CommonPage.restPage(pageList));
    }

    @PostMapping("/add")
    @ApiOperation("add => 新增物品申领")
    public CommonResult<CommonPage<MaterialCollectVO>> addMaterialCollect(@RequestBody MaterialCollectVO vo) throws ApiException {
        int num =  materialCollectService.addOrUpdateMaterialCollect(vo);
        if(num>0){
            return CommonResult.success();
        }
        return  CommonResult.failed();
    }

    @PostMapping("/update")
    @ApiOperation("update => 修改物品申领")
    @SysLog
    public CommonResult<CommonPage<MaterialCollectVO>> updateMaterialCollect(@RequestBody MaterialCollectVO vo) throws ApiException {
        int num =  materialCollectService.addOrUpdateMaterialCollect(vo);
        if(num>0){
            return CommonResult.success();
        }
        return  CommonResult.failed();
    }

    @PostMapping("/delete")
    @ApiOperation("delete => 删除物品申领")
    @SysLog
    public CommonResult<CommonPage<MaterialCollectVO>> deleteMaterialCollect(@RequestBody MaterialCollectVO vo) throws ApiException {
        int num =  materialCollectService.deleteMaterialCollectById(vo.getId());
        if(num>0){
            return CommonResult.success();
        }
        return  CommonResult.failed();
    }

    @PostMapping("/repay")
    @ApiOperation("repay => 归还物品")
    public CommonResult<CommonPage<MaterialCollectVO>> repayProcess(@RequestBody MaterialCollectVO vo) throws ApiException {
        int num =  materialCollectService.repayProcess(vo.getId());
        if(num>0){
            return CommonResult.success();
        }
        return  CommonResult.failed();
    }



}
